//
//  accountsToolBar.m
//  测试
//
//  Created by 狮子座 on 2017/4/20.
//  Copyright © 2017年 森之源. All rights reserved.
//

#import "accountsToolBar.h"
#import "UIButton+OF.h"
#import <Masonry/Masonry.h>
#import "NSString+Tools.h"

@interface accountsToolBar()
///合计
@property (strong,nonatomic) UILabel *totalLabel;

@end
@implementation accountsToolBar
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self _init];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self _init];
    }
    return self;
}

-(void)_init
{
    UIFont *font = [UIFont systemFontOfSize:18.f];
    CGFloat width = [NSString calculateLabelWidth:@"合计:¥" font:font AndHeight:20];
    
    //删除按钮
    [self addSubview:self.deleteButton];
    WSELF(weakSelf);
    [self.deleteButton setBackgroundColor:[UIColor yellowColor]];
    [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        SSELF(strongSelf);
        make.width.equalTo(@55);
        make.height.equalTo(@30);
        make.left.equalTo(strongSelf.mas_left).offset(10);
        make.centerY.equalTo(strongSelf);
    }];
    
    
    
    
    //全选按钮
    [self addSubview:self.allButton];
    [self.allButton setBackgroundColor:[UIColor yellowColor]];
    [self.allButton mas_makeConstraints:^(MASConstraintMaker *make) {
        SSELF(strongSelf);
        make.width.equalTo(@55);
        make.height.equalTo(@30);
        make.left.equalTo(strongSelf.deleteButton.mas_right).offset(10);
        make.centerY.equalTo(strongSelf);
    }];
    
    
    //合计
    [self addSubview:self.totalLabel];
    [self.totalLabel setBackgroundColor:[UIColor yellowColor]];
    [self.totalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        SSELF(strongSelf);
        make.width.equalTo(@(width+2));
        make.height.equalTo(@20);
        make.left.equalTo(strongSelf.allButton.mas_right).offset(10);
        make.centerY.equalTo(strongSelf);
    }];
    
    
    //去结算
    [self addSubview:self.accountsButton];
    [self.accountsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        SSELF(strongSelf);
        make.width.equalTo(@60);
        make.height.equalTo(@30);
        make.right.equalTo(strongSelf.mas_right).offset(-10);
        make.centerY.equalTo(strongSelf);
    }];

    
    
    //金额
    [self addSubview:self.priceLabel];
     [self.priceLabel setBackgroundColor:[UIColor yellowColor]];
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        SSELF(strongSelf);
        make.height.equalTo(@20);
        make.left.equalTo(strongSelf.totalLabel.mas_right).offset(5);
        make.right.equalTo(strongSelf.accountsButton.mas_left).offset(-5);
        make.centerY.equalTo(strongSelf);
    }];

    
    
    
    

    
    
    
}

-(void)onUpdatePrice:(NSString *)price
{
    [self.priceLabel setText:price];
}


#pragma makr - 删除按钮
-(UIButton *)deleteButton
{
    if (!_deleteButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setNormalTitle:@"删除"];
        [button setNormalTitleColor:[UIColor lightGrayColor]];
        [button addTarget:self action:@selector(onDelete:)];
        [button setTitleFont:15.f];
        _deleteButton = button;
    }
    return _deleteButton;
}

-(void)onDelete:(UIButton *)button
{
    
    
    if ([self.delegate respondsToSelector:@selector(onDeleteCommodity:)]) {
         [self.delegate onDeleteCommodity:button];
    }
    if ([self.delegate respondsToSelector:@selector(onSelectType:Button:)]) {
        [self.delegate onSelectType:selectToolTypeDelete Button:button];
    }
}


#pragma makr - 全选按钮
-(UIButton *)allButton
{
    if (!_allButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setNormalTitle:@"全选"];
        [button setNormalTitleColor:[UIColor lightGrayColor]];
        [button addTarget:self action:@selector(onAll:)];
        [button setTitleFont:15.f];
        _allButton = button;
    }
    return _allButton;
}
-(void)onAll:(UIButton *)button
{
    if ([self.delegate respondsToSelector:@selector(onAllCommodity:)]) {
        [self.delegate onAllCommodity:button];
    }

    button.selected =!button.selected;
    [button setNormalTitle:button.selected?@"反全选":@"全选"];
    
    if ([self.delegate respondsToSelector:@selector(onSelectType:Button:)]) {
        [self.delegate onSelectType:button.selected?selectToolTypeAll:selectToolTypeAllNo Button:button];
    }
}

#pragma makr - 结账按钮
-(UIButton *)accountsButton
{
    if (!_accountsButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setNormalTitle:@"去结算"];
        [button setNormalTitleColor:[UIColor whiteColor]];
        [button setBackgroundColor:[UIColor redColor]];
        [button addTarget:self action:@selector(onAccounts:)];
        button.titleLabel.font = [UIFont boldSystemFontOfSize:18.f];
        _accountsButton = button;
    }
    return _accountsButton;
}
-(void)onAccounts:(UIButton *)button
{
    if ([self.delegate respondsToSelector:@selector(onAccountsCommodity:)]) {
        [self.delegate onAccountsCommodity:button];
    }
    
    if ([self.delegate respondsToSelector:@selector(onSelectType:Button:)]) {
        [self.delegate onSelectType:selectToolTypeAccounts Button:button];
    }

    
    
}


#pragma makr - 结算金额
-(UILabel *)priceLabel
{
    if (!_priceLabel) {
        UILabel *label = [[UILabel alloc]init];
        [label setText:@"0:00"];
        [label setTextColor:[UIColor redColor]];
        [label setTextAlignment:NSTextAlignmentLeft];
        [label setFont:[UIFont boldSystemFontOfSize:14]];
        _priceLabel = label;
    }
    return _priceLabel;
}

#pragma makr - 合计
-(UILabel *)totalLabel
{
    if (!_totalLabel) {
        UILabel *label = [[UILabel alloc]init];
        [label setText:@"合计:¥"];
        [label setTextColor:[UIColor redColor]];
        [label setTextAlignment:NSTextAlignmentCenter];
        [label setFont:[UIFont boldSystemFontOfSize:18]];
        _totalLabel = label;
    }
    return _totalLabel;
}

@end
